package com.controller;

import com.bean.Customer;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

@Controller
@ResponseBody
public class JsonController {

    @RequestMapping("/json")
    public String JsonTest() {
        Customer cu1 = new Customer();
        cu1.setId(11);
        cu1.setUname("admin7");
        cu1.setPassword("7777");
        cu1.setAge(20);
        cu1.setEmail("xx@163.com");

        Customer cu2 = new Customer();
        cu2.setId(22);
        cu2.setUname("admin7-2");
        cu2.setPassword("7777-2");
        cu2.setAge(21);
        cu2.setEmail("xx@163.com-2");

        Customer cu3 = new Customer();
        cu3.setId(33);
        cu3.setUname("333");
        cu3.setPassword("123456");
        cu3.setAge(20);
        cu3.setEmail("xx@126.com");

        List<Customer> list = new ArrayList<>();
        list.add(cu1);
        list.add(cu2);
        list.add(cu3);

        ObjectMapper obj = new ObjectMapper();

        try {
            //对象结构
//            return obj.writeValueAsString(cu1);
            //数据结构
            return obj.writeValueAsString(list);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return null;

    }
}
